package kr.co.bettersoft.waitnumberstorem.adapter;
/**
 * WaitingUserListAdapter
 * 
 * 메인 - 홈 화면 하단에 대기 고객 리스트를 보여줄때 사용하는 아답터
 */
import java.util.List;

import kr.co.bettersoft.waitnumberstorem.activity.R;
import kr.co.bettersoft.waitnumberstorem.common.CommonConstant;
import kr.co.bettersoft.waitnumberstorem.domain.WaitingUser;
import kr.co.bettersoft.waitnumberstorem.fragment.WaitUserListFragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class WaitingUserListAdapter extends BaseAdapter {
	String TAG = "WaitingUserListAdapter";
	private Context context;
	private final List<WaitingUser> entries;
	private WaitUserListFragment receiveWaitUserListFragment;
	
	// 폰트
	private Typeface typefaceNanumGothic= null; 
	private Typeface typefaceNanumGothicBold = null;
	
	public WaitingUserListAdapter(Context context, WaitUserListFragment waitUserListFragment, List<WaitingUser> entriesFn) {		
		this.context = context;
		this.entries = entriesFn;
		this.receiveWaitUserListFragment = waitUserListFragment;
		
		// 폰트 지정.
	    typefaceNanumGothic = Typeface.createFromAsset(context.getAssets(), CommonConstant.NanumGothic);  
	    typefaceNanumGothicBold = Typeface.createFromAsset(context.getAssets(), CommonConstant.NanumGothicBold);
	}
	Boolean blued = false;
	public View getView(int position, View convertView, ViewGroup parent) {
		
		//Log.d(TAG,"position::"+position);
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View listView;
		if (convertView == null) {
			listView = new View(context);
			listView = inflater.inflate(R.layout.waiting_user_list_adapter, null);
		} else {
			listView = (View) convertView;
		}
 
		// set value into textview
		
		TextView waitNumber = (TextView) listView.findViewById(R.id.waiting_number);
		TextView customerId = (TextView) listView.findViewById(R.id.customer_id);		
		TextView customerPersonCnt = (TextView) listView.findViewById(R.id.customer_companion);		
		TextView companionAfterText = (TextView) listView.findViewById(R.id.companion_after_text);		
		TextView registerDate = (TextView) listView.findViewById(R.id.register_date);
		
		// 대기 취소 버튼
		View customerWaitBtnLayout = listView.findViewById(R.id.customer_wait_btn_layout);
		Button customerWaitCancelBtn = (Button) listView.findViewById(R.id.customer_wait_cancel_btn);
		customerWaitCancelBtn.setTag(position);
		if(((WaitingUser)entries.get(position)).getShowCancelBtnYn()){
			customerWaitBtnLayout.setVisibility(View.VISIBLE);
		}else{
			customerWaitBtnLayout.setVisibility(View.GONE);
		}
		customerWaitCancelBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				receiveWaitUserListFragment.cancelPosition = Integer.parseInt(v.getTag()+"");
				receiveWaitUserListFragment.confirmAndCancelUser();
			}
		});
		
		if(position==0){		// 첫항목 
			listView.setBackgroundColor(Color.YELLOW);
		}else{
			listView.setBackgroundColor(Color.WHITE);
		}
		waitNumber.setText(((WaitingUser)entries.get(position)).getNumberWaiting());		// 대기 번호
		String tempUserName = ((WaitingUser)entries.get(position)).getUserName();
		String tempPhoneNumber = ((WaitingUser)entries.get(position)).getPhoneNumber();
		String tempQrCode = ((WaitingUser)entries.get(position)).getQrCode();
		if(tempUserName.length()>0){					// 전번이 있으면 전번으로 출력
			customerId.setText(tempUserName);
		}else if(tempPhoneNumber.length()>0){											// 전번이 없으면 아이디로 출력
			customerId.setText(tempPhoneNumber);
		}else if(tempQrCode.length()>0){
			customerId.setText(tempQrCode);
		}else{
			customerId.setText(context.getString(R.string.no_info));	 
		}

		customerPersonCnt.setText(((WaitingUser)entries.get(position)).getCompanion());
		registerDate.setText(((WaitingUser)entries.get(position)).getRegisterDate());

		// 폰트 적용
		waitNumber.setTypeface(typefaceNanumGothicBold);		 
		customerId.setTypeface(typefaceNanumGothic);		 
		customerPersonCnt.setTypeface(typefaceNanumGothic);		 
		companionAfterText.setTypeface(typefaceNanumGothic);		 
		registerDate.setTypeface(typefaceNanumGothic);		
		
		return listView;
	}
 
	@Override
	public int getCount() {
		return entries.size();
	}
 
	@Override
	public Object getItem(int position) {
		return null;
	}
 
	@Override
	public long getItemId(int position) {
		return 0;
	}
 
}